﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BYS.XiAnComDept.BussLogic;

namespace BYS.XiAnComDept.App.Pages
{
    /// <summary>
    /// Interaction logic for DataBackup.xaml
    /// </summary>
    public partial class DataBackup : UserControl
    {
        BllFactory bllFactory = new BllFactory();

        public DataBackup()
        {
            InitializeComponent();
            this.InitListViewBackupFiles();
        }

        public void InitListViewBackupFiles()
        {
            string[] allBackupFiles = bllFactory.Db.GetAllBackupFiles();
            this.ListViewBackupFiles.ItemsSource = allBackupFiles;
        }

        private void buttonBackup_Click(object sender, RoutedEventArgs e)
        {
            string backupFile = bllFactory.Db.BackupDb();
            MessageBox.Show("备份成功！\n备份文件名为：" + backupFile + "\n。", "备份成功！");
            InitListViewBackupFiles();
        }

        private void buttonRestore_Click(object sender, RoutedEventArgs e)
        {
            if (this.ListViewBackupFiles.SelectedItems.Count == 1)
            {
                string filePath = this.ListViewBackupFiles.SelectedItems[0].ToString();
                if(MessageBox.Show(
                    string.Format("你确定要使用备份数据{0}来恢复数据吗？\n点击确认后，数据库将恢复到备份时情形，备份后所做的所有修改将丢失！\n恢复成功后，软件将自动关闭，请重新打开！", filePath), 
                    "确认？",
                    MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                bllFactory.Db.RestoreDb(this.ListViewBackupFiles.SelectedItems[0].ToString());
                MessageBox.Show("恢复成功！\n软件即将自动关闭！", "恢复成功！");
                Application.Current.Shutdown();
            }
            else
            {
                MessageBox.Show("请选择你要恢复的数据库文件", "提示");
            }
        }
    }
}
